package org.testis.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.Properties;

/**
 *
 * @author nurhat
 */
public class PropertyFileProvider {

    private Properties m_prop;
    private boolean m_fromJarFile;
    private String m_fullPathToPropFile;

    public PropertyFileProvider() {
        m_prop = new Properties();
        m_fullPathToPropFile = "";
        m_fromJarFile = false;
    }

    public void listProperties(PrintStream out) {
        m_prop.list(out);

    }

    public boolean loadPropertiesFile(String fullPathToPropFile,
            boolean fromCurrentJarFile) {
        this.m_fullPathToPropFile = fullPathToPropFile;
        m_fromJarFile = fromCurrentJarFile;
        try {
            if (!m_fromJarFile) {
                File f = new File(fullPathToPropFile);
                if (f.exists()) {
                    m_prop.load(new FileInputStream(fullPathToPropFile));
                } else {
                    return false;
                }
            } else {

                URL url =
                        ClassLoader.getSystemResource(fullPathToPropFile);
                if (url != null) {
                    m_prop.load(url.openStream());
                }

            }
            return true;
        } catch (IOException ex) {
            System.out.println("loadPropertiesFile:" + ex.toString());
        }
        return false;
    }

    public String getProperty(String propertyName) {
        if (m_fullPathToPropFile.equals("")) {
            return null;
        }
        return m_prop.getProperty(propertyName);
    }

    public void removeProperty(String propertyName) {
        if (m_fullPathToPropFile.equals("")) {
            return;
        }
        m_prop.remove(propertyName);
    }

    public Enumeration<String> getPropertyNames() {
        if (m_fullPathToPropFile.equals("")) {
            return null;
        }
        return (Enumeration<String>) m_prop.propertyNames();
    }

    public boolean putProperty(String propertyName,
            String propertyValue,
            String propertyComment) {
        boolean result = false;
        if (m_fullPathToPropFile.equals("")) {
            return result;
        }
        if (!m_fromJarFile) {
            FileOutputStream out = null;
            try {
                m_prop.put(propertyName,
                        propertyValue);
                out = new FileOutputStream(this.m_fullPathToPropFile);
                m_prop.store(out,
                        propertyComment);

                result = true;
            } catch (FileNotFoundException ex) {
                System.out.println("putProperty:" + ex.toString());
            } catch (IOException ex) {
                System.out.println("putProperty:" + ex.toString());
            } finally {
                try {
                    out.close();
                } catch (IOException ex) {
                    System.out.println("putProperty:" + ex.toString());
                }
            }
        }
        return result;
    }
}
